﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Ch.Admin.Bfs.Sms.Sdmx.Utils;
using System.Collections.Generic;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Control
{
    /// <summary>
    /// User control for property Grid allowing edition of Annotation item translation definition
    /// </summary>
    public partial class UcAnnotationItemRename : UserControl
    {
        #region Constants
        private const string LANG_DE = "de";
        private const string LANG_FR = "fr";
        private const string LANG_IT = "it";
        private const string LANG_EN = "en";
        #endregion

        #region Property exchange for property with UITypeEditor

        public IWindowsFormsEditorService _wfes;

        public SerializableDictionary<string, InternationalizedString> DicoReplace { get; set; }

        private DataTable tab = null;
        #endregion

        #region Constructors

        public UcAnnotationItemRename()
        {
            DicoReplace = new SerializableDictionary<string, InternationalizedString>();
            Init();
        }

        public UcAnnotationItemRename(SerializableDictionary<string, InternationalizedString> dico)
        {
            DicoReplace = dico == null ? new SerializableDictionary<string, InternationalizedString>() : dico;
            Init();
        }

        private void Init()
        {
            InitializeComponent();
            tab = new DataTable();
            tab.Columns.Add(new DataColumn("Item"));
            tab.Columns.Add(new DataColumn(LANG_EN));
            tab.Columns.Add(new DataColumn(LANG_DE));
            tab.Columns.Add(new DataColumn(LANG_FR));
            tab.Columns.Add(new DataColumn(LANG_IT));

            foreach (KeyValuePair<string, InternationalizedString> kvp in DicoReplace)
            {
                tab.Rows.Add(new object[] { kvp.Key, kvp.Value[LANG_EN], kvp.Value[LANG_DE], kvp.Value[LANG_FR], kvp.Value[LANG_IT] });
            }
            this.dataGridView.DataSource = tab;

        }
        #endregion

        #region Leaving Event

        private void UcAnnotationItemRename_Leave(object sender, EventArgs e)
        {
            dataGridView.EndEdit();
            dataGridView.CurrentCell = null;
            (dataGridView.DataSource as DataTable).AcceptChanges();

            if (DicoReplace != null)
            {
                DicoReplace.Clear();
                foreach (DataRow r in tab.Rows)
                {
                    string item = r[0].ToString().Trim();
                    if (item.Length > 0 && DicoReplace.ContainsKey(item) == false)
                    {
                        InternationalizedString istr = new InternationalizedString();
                        istr[LANG_EN] = r[LANG_EN].ToString().Trim();
                        istr[LANG_DE] = r[LANG_DE].ToString().Trim();
                        istr[LANG_FR] = r[LANG_FR].ToString().Trim();
                        istr[LANG_IT] = r[LANG_IT].ToString().Trim();
                        DicoReplace.Add(item, istr);
                    }
                }
            }
        }
        #endregion
    }
}
